# author: axin
# date: 2021/4/23 15:04
# description:
from rest_framework.decorators import action
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from django.contrib.auth.models import Permission, ContentType

from meiduo_admin.serialziers.permissions import PermissionsSerializers, ContentTypeSerializers
from meiduo_admin.utils import PageNum


class PermissionsView(ModelViewSet):
    """
        权限视图表
    """
    queryset = Permission.objects.all()
    serializer_class = PermissionsSerializers
    pagination_class = PageNum
    permission_classes = [IsAdminUser]

    # 父类中没有权限类型表的操作，需要自己封装方法
    def content_type(self, request):
        """
            获取权限类型
        :param request:
        :return:
        """
        # 1. 获取权限类型的所有数据
        data = ContentType.objects.all()
        # 2. 序列化返回权限类型 self.get_serializer() 获取serializer_ lass 属性所指定的序列化器对象
        ser = ContentTypeSerializers(data, many=True)
        # 响应对象
        return Response(ser.data)

    def simple(self, request):
        """
            获取权限数据
        :param request:
        :return:
        """
        # 1. 查询权限表
        data = Permission.objects.all()
        # 2. 返回权限数据
        ser = PermissionsSerializers(data, many=True)
        return Response(ser.data)
